//
//  AppDelegate.m
//  loveBeen
//
//  Created by 杨烁 on 2016/11/19.
//  Copyright © 2016年 loveBeen. All rights reserved.
//

#import "AppDelegate.h"
#import "LBTabBarController.h"
#import "ADView.h"
#import "ADViewController.h"
#import "HcdGuideView.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self adData];

    BMKMapManager *_mapManager;
    
    _mapManager = [[BMKMapManager alloc]init];
    
    BOOL ret = [_mapManager start:@"zkRmAnWRPxFaHB8LLKeRS2ZKFuXIuBXm" generalDelegate:nil];
    if (!ret) {
        NSLog(@"manager start failed!");
    }

    [self launchingVersion];
    
    return YES;
}


 

//版本判断,启动判断,新特性加广告
-(void)launchingVersion{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSString *version = [[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleShortVersionString"];
    //根据版本号来区分是否要显示引导图
    BOOL show = [ud boolForKey:[NSString stringWithFormat:@"Guide_%@", version]];
    //根据是不是第一次启动app
    BOOL isFirstLogin = [[[NSUserDefaults standardUserDefaults] objectForKey:@"isFirstLogin"] boolValue];
    NSString *imagePath =[[NSUserDefaults standardUserDefaults]objectForKey:@"imagePath"];
    

    NSString *adPath =[[NSUserDefaults standardUserDefaults]objectForKey:@"adPath"];
    
    if ((!isFirstLogin)||(!show)) {
        //是第一次   新特性界面(缺少新特性界面)
        
        [self enterMain];
        [self feature];
        //        [self showAD:imagePath withAddress:adPath];
        [ud setBool:YES forKey:[NSString stringWithFormat:@"Guide_%@", version]];
        [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"isFirstLogin"];
        
    }else{
        //不是首次启动
        [self enterMain];
        
        [self showAD:imagePath withAddress:adPath];

        //        [self enterMain];
//        NSLog(@"1---%@",[NSThread currentThread]);

        

    }
}

//新特性界面
-(void)feature{
    
    NSMutableArray *images = [NSMutableArray new];
    
    [images addObject:[UIImage imageNamed:@"guide_40_1"]];
    [images addObject:[UIImage imageNamed:@"guide_40_2"]];
    [images addObject:[UIImage imageNamed:@"guide_40_3"]];
    [images addObject:[UIImage imageNamed:@"guide_40_4"]];
    HcdGuideView *guideView = [HcdGuideView sharedInstance];
    guideView.window = self.window;
    [guideView showGuideViewWithImages:images
                        andButtonTitle:@"立即进入"
                   andButtonTitleColor:[UIColor whiteColor]
                      andButtonBGColor:[UIColor clearColor]
                  andButtonBorderColor:[UIColor whiteColor]];



    

}

//设置主窗口,启动主界面
-(void)enterMain{
    _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    //2.创建视图控制器
    LBTabBarController *tabBarController = [[LBTabBarController alloc]init];
    //3.把视图控制器设置为window的根视图控制器
    _window.rootViewController = tabBarController;
    
    //4.设置window显示
    [_window makeKeyAndVisible];
    [[UITabBar appearance]setTintColor:[UIColor grayColor]];
    


    
    

}

//网络获取广告数据
- (void)adData{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:@"7" forKey:@"call"];
    
    [DSHTTPClient postUrlString:@"ad.json.php" withParam:param withSuccessBlock:^(id data) {
        
        NSString *image = data[@"data"][@"img_big_name"];
        NSString *ad = @"http://www.baidu.com/";
        
        [[NSUserDefaults standardUserDefaults] setValue:image forKey:@"adImageName"];
        [[NSUserDefaults standardUserDefaults] setValue:ad forKey:@"adPath"];
        [[ADView alloc] setNewADImgUrl:image];
        
    } withFailedBlock:^(NSError *error) {
        
    } withErrorBlock:^(NSString *message) {
        
    }];
    
}

//显示广告
-(void)showAD:(NSString *)image withAddress:(NSString *)ad{
    
    //1、创建广告
    ADView *adView = [[ADView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.bounds imgUrl:image adUrl:ad clickImg:^(NSString *clikImgUrl) {
        NSLog(@"进入广告:%@",clikImgUrl);
        ADViewController *adVc = [[ADViewController alloc] init];
        adVc.adUrl = clikImgUrl;
        [[UINavigationController alloc] pushViewController:adVc animated:YES];
        
    }];
    //设置倒计时（默认3秒）
    adView.showTime = 5;
    //2、显示广告
    [adView show];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
